import TreeCesiumData from "./layer_tree_data";
import Environment from "../../utils/environment";
import LayerManager from "@/plugins/mev/layer/LayerManager";
import axios from "axios"
import * as Cesium from "cesium";

class LabelData extends TreeCesiumData {
    constructor(data, viewer) {
        super(data, viewer);
        this._model = null;
        this._data = data;
        this._viewer = viewer;
        this._dataObject = null;
        this._rect = null;
        this._primitiveIndex = 0;
    }

    show() {

        let labels = new Cesium.BillboardCollection();
        let options = this._data.options;
        const fillColor = options.fillColor !== undefined ? Cesium.Color.fromCssColorString(options.fillColor) : Cesium.Color.fromByte(0, 0, 0 , 255);
        const outlineColor = options.outlineColor !== undefined ? Cesium.Color.fromCssColorString(options.outlineColor) : undefined;
        const backgroundColor = options.backgroundColor !== undefined ? Cesium.Color.fromCssColorString(options.backgroundColor) : Cesium.Color.fromBytes(0, 0, 0, 0);
        const backgroundPadding = options.backgroundPadding !== undefined ? new Cesium.Cartesian2(options.backgroundPadding[0], options.backgroundPadding[1]) : undefined;
        const pixelOffset = options.pixelOffset !== undefined ? new Cesium.Cartesian2(options.pixelOffset[0], options.pixelOffset[1]) : undefined;
        const eyeOffset = options.eyeOffset !== undefined ? new Cesium.Cartesian2(options.eyeOffset[0], options.eyeOffset[1]) : undefined;
        const labelField = this._data.labelField !== undefined ? this._data.labelField : "name";

        // For canvas context
        let font = options.font;
        if(font === undefined)
        {
            const fontStyle = options.fontStyle !== undefined ? options.fontStyle : "normal";
            const fontSize = options.fontSize !== undefined ? options.fontSize : 12;
            const fontFamily = options.fontFamily !== undefined ? options.fontFamily : "MicroSoft YaHei";
            font = fontStyle + " " + fontSize + "px " + fontFamily
        }

        const labelBackgroundColorCanvas = options.backgroundColor !== undefined ? options.backgroundColor : "#00000000";
        const labelColorCanvas = options.fillColor !== undefined ? options.fillColor : "#000000FF";
        //console.log(canvastext.toDataURL('image/png'))
        var canvastext = document.createElement('canvas');
        canvastext.width = 200;
        canvastext.height = 100;
        var ctx = canvastext.getContext('2d');
        let that = this;
        axios.get(this._data.basePath + this._data.path + "?datatype=json").then(function (response) {
                let json = response.data;
                let west = 180.0;
                let east = -180.0;
                let north = -90.0;
                let south = 90.0;
                json.features.forEach(function (feature) {
                    let name = feature.properties[labelField];
                    ctx.font = font;
                    let width = ctx.measureText(name).width + 10;
                    let height = 30;
                    canvastext.width = width;
                    canvastext.height = height;
                    ctx.fillStyle = labelBackgroundColorCanvas;
                    ctx.fillRect(0, 0, canvastext.width, canvastext.height);
                    ctx.fillStyle = labelColorCanvas;
                    ctx.textBaseline = 'middle';
                    ctx.textAlign = 'center';
                    ctx.fillText(name, canvastext.width / 2, canvastext.height / 2)
                    labels.add({
                        position: Cesium.Cartesian3.fromDegrees(
                            feature.geometry.coordinates[0],
                            feature.geometry.coordinates[1],
                            100
                        ),
                        image: ctx.getImageData(0, 0, width, height),
                        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 10000)
                    });

                    if (feature.geometry.coordinates[0] < west) {
                        west = feature.geometry.coordinates[0];
                    }
                    if (feature.geometry.coordinates[0] > east) {
                        east = feature.geometry.coordinates[0];
                    }
                    if (feature.geometry.coordinates[1] < south) {
                        south = feature.geometry.coordinates[1];
                    }
                    if (feature.geometry.coordinates[1] > north) {
                        north = feature.geometry.coordinates[1];
                    }

                });
            that._rect = new Cesium.Rectangle(Cesium.Math.toRadians(west), Cesium.Math.toRadians(south), Cesium.Math.toRadians(east), Cesium.Math.toRadians(north));
            that._dataObject = labels;
            that._viewer.scene.primitives.add(labels, that._primitiveIndex);
            }
        );
    }

    close() {
        if (this._dataObject) {
            if (this._viewer.scene.primitives.contains(this._dataObject)) {
                this._viewer.scene.primitives.remove(this._dataObject);
            }
            this._dataObject = null;
        }
    }

    flyTo() {
        if (this._rect !== null) {
            this._viewer.camera.flyTo({
                destination: this._rect
            });
        }
    }

    updateInternalIndex(internalIndex){
        if(this._data.visible)
        {
            this._primitiveIndex = internalIndex.primitiveIndex;
            ++internalIndex.primitiveIndex;
        }
    }
}

export default LabelData;
